python學(xué)習(xí)日志列表list操作
發(fā)布時(shí)間:2024/11/19 23:57:43
列表的常用操作
列表的作⽤是⼀次性存儲(chǔ)多個(gè)數(shù)據(jù),程序員可以對這些數(shù)據(jù)進(jìn)⾏的操作有:增、刪、改、查。
name_list = ['Tom', 'Lily', 'Rose']
print(name_list[0]) # Tom
print(name_list[1]) # Lily
print(name_list[2]) # Rose
=======================
切片
data_lsit = [i for i in range(1, 21)]
print(data_lsit) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
=================================
# 切片的常規(guī)操作
print(data_lsit[::]) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print(data_lsit[::-1]) # [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
print(data_lsit[::2]) # 當(dāng)前步長為2代表獲取列表下標(biāo)為偶數(shù)的元素 # [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(data_lsit[:10]) # 獲取到元素下標(biāo)為10之前的所有元素 # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(data_lsit[1:10]) # [2, 3, 4, 5, 6, 7, 8, 9, 10]
=========================
index():返回指定數(shù)據(jù)所在位置的下標(biāo)
name_list = ['Tom', 'Lily', 'Rose']
print(name_list.index('Lily', 0, 2)) # 1
==========================
count():統(tǒng)計(jì)指定數(shù)據(jù)在當(dāng)前列表中出現(xiàn)的次數(shù)
name_list = ['Tom', 'Lily', 'Rose']
print(name_list.count('Lily')) # 1
==========================
len(): 訪問列表⻓度,即列表中數(shù)據(jù)的個(gè)數(shù)。
name_list = ['Tom', 'Lily', 'Rose']
print(len(name_list)) # 3
==========================
in :判斷指定數(shù)據(jù)在某個(gè)列表序列,如果在返回 True ,否則返回 False
name_list = ['Tom', 'Lily', 'Rose']
# 結(jié)果:True
print('Lily' in name_list)
# 結(jié)果:False
print('Lilys' in name_list)
==========================
需求:查找⽤戶輸⼊的名字是否已經(jīng)存在
name_list = ['Tom', 'Lily', 'Rose']
name = input('請輸⼊您要搜索的名字:')
if name in name_list:
print(f'您輸⼊的名字是{name}, 名字已經(jīng)存在')
else:
print(f'您輸⼊的名字是{name}, 名字不存在')
==========================
append():列表結(jié)尾追加數(shù)據(jù)
name_list = ['Tom', 'Lily', 'Rose']
name_list.append('xiaoming')
# 結(jié)果:['Tom', 'Lily', 'Rose', 'xiaoming']
print(name_list)
==========================
extend():列表結(jié)尾追加數(shù)據(jù),如果數(shù)據(jù)是⼀個(gè)序列,則將這個(gè)序列的數(shù)據(jù)逐⼀添加到列表。
name_list = ['Tom', 'Lily', 'Rose']
name_list.extend('xiaoming')
# 結(jié)果:['Tom', 'Lily', 'Rose', 'x', 'i', 'a', 'o', 'm', 'i', 'n', 'g']
print(name_list)
==========================
insert():指定位置新增數(shù)據(jù)。
name_list = ['Tom', 'Lily', 'Rose']
name_list.insert(1, 'xiaoming')
# 結(jié)果:['Tom', 'xiaoming', 'Lily', 'Rose']
print(name_list)
==========================
del 刪除⽬標(biāo)
name_list = ['Tom', 'Lily', 'Rose']
# 結(jié)果:報(bào)錯(cuò)提示:name 'name_list' is not defined
del name_list # 【已經(jīng)刪除了,所以打印不出來】【也可以這樣寫:del (name_list)】
print(name_list)
==========================
刪除指定數(shù)據(jù)
name_list = ['Tom', 'Lily', 'Rose']
del name_list[0]
# 結(jié)果:['Lily', 'Rose']
print(name_list)
==========================
remove():移除列表中某個(gè)數(shù)據(jù)的第⼀個(gè)匹配項(xiàng)。、
name_list = ['Tom', 'Lily', 'Rose']
name_list.remove('Rose') # 【這兒只能放一個(gè)參數(shù),多了報(bào)錯(cuò)】
# 結(jié)果:['Tom', 'Lily']
print(name_list)
==========================
clear():清空列表 【只是清空,列表還是存在的】
name_list = ['Tom', 'Lily', 'Rose']
name_list.clear()
print(name_list) # 結(jié)果: []
==========================
排序:sort()
reverse表示排序規(guī)則,reverse = True 降序, reverse = False 升序(默認(rèn))
num_list1 = [1, 5, 2, 3, 6, 8]
num_list1.sort()
# 結(jié)果:[1, 2, 3, 5, 6, 8]
print(num_list1)
num_list2 = [1, 5, 2, 3, 6, 8]
num_list2.sort(reverse=True)
# 結(jié)果:[8, 6, 5, 3, 2, 1]
print(num_list2)
==========================
for函數(shù)
name_list = ['Tom', 'Lily', 'Rose']
for i in name_list:
print(i)